﻿using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using BTechBids.DataModels;
using BTechBids.Data;
using System.Net.Mail;
using System.Net;
using System.Configuration;
using System.Web.Configuration;
using System;

namespace BTechBids.Web
{
    // Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.

    public class ApplicationUserManager : UserManager<ApplicationUser>
    {
        public ApplicationUserManager(IUserStore<ApplicationUser> store)
            : base(store)
        {
        }

        public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
        {
            var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
            // Configure validation logic for usernames
            manager.UserValidator = new UserValidator<ApplicationUser>(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };
            // Configure validation logic for passwords
            manager.PasswordValidator = new PasswordValidator
            {
                RequiredLength = 6,
                RequireNonLetterOrDigit = false,
                RequireDigit = false,
                RequireLowercase = false,
                RequireUppercase = false,
            };
            manager.EmailService = new EmailService();
            //manager.SmsService = new SmsService();
            var dataProtectionProvider = options.DataProtectionProvider;
            if (dataProtectionProvider != null)
            {
                manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
            }
            return manager;
        }
    }
    public class EmailService : IIdentityMessageService
    {
        public Task SendAsync(IdentityMessage message)
        {
            // Plug in your email service here to send an email.

            

            var gmailUserName = ConfigurationManager.AppSettings["GmailUserName"];
            var gmailPassword = ConfigurationManager.AppSettings["GmailPassword"];
            var gmailHost = ConfigurationManager.AppSettings["GmailHost"];
            var gmailPort = Int32.Parse(ConfigurationManager.AppSettings["GmailPort"]);
            var gmailSsl = Boolean.Parse(ConfigurationManager.AppSettings["GmailSsl"]);

            var client = new SmtpClient(gmailHost, gmailPort)
            {
                Credentials = new NetworkCredential(gmailUserName, gmailPassword),
                EnableSsl = gmailSsl
            };

            var mm = new MailMessage(gmailUserName, message.Destination, message.Subject, message.Body);
            mm.IsBodyHtml = true;

                
            //client.Send(gmailUserName, message.Destination, message.Subject, message.Body);
            client.Send(mm);
            
            

            return Task.FromResult(0);
        }

       
    }
}
